package com.shd.edanpei.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.MyApplication;
import com.shd.edanpei.base.TranBarActivity;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.receiver.ListenSecondService;
import com.shd.edanpei.receiver.SecondService;
import com.shd.edanpei.ui.fragment.HomeFragment;
import com.shd.edanpei.ui.fragment.ManagerFragment;
import com.shd.edanpei.ui.fragment.MineFragment;
import com.shd.edanpei.ui.fragment.PublishNewFragment;
import com.shd.edanpei.ui.fragment.ShareOrderNewFragment;
import com.shd.edanpei.util.FragmentUtil;
import com.shd.edanpei.util.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;


public class MainActivity extends TranBarActivity {

    @BindView(R.id.iv_menu1)
    ImageView ivMenu1;
    @BindView(R.id.iv_menu2)
    ImageView ivMenu2;
    @BindView(R.id.iv_menu3)
    ImageView ivMenu3;
    @BindView(R.id.iv_menu4)
    ImageView ivMenu4;
    @BindView(R.id.iv_menu5)
    ImageView ivMenu5;
    @BindView(R.id.tv_menu1)
    TextView tvMenu1;
    @BindView(R.id.tv_menu2)
    TextView tvMenu2;
    @BindView(R.id.tv_menu3)
    TextView tvMenu3;
    @BindView(R.id.tv_menu4)
    TextView tvMenu4;
    @BindView(R.id.tv_menu5)
    TextView tvMenu5;


    private FragmentUtil fragmentUtil;
    private List<ImageView> imageViews = new ArrayList<>();
    private Animation operatingAnim;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        fragmentUtil = new FragmentUtil(getSupportFragmentManager(), R.id.frame_layout);
        changeTab(R.id.navigation_tab1);
        JPushInterface.setAlias(MyApplication.getApplication(), DaoManager.getInstance().getUserBean().getId() + "", null);
        startService(new Intent(this,ListenSecondService.class));
        startService(new Intent(this,SecondService.class));
         operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_main_anim);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);


    }

    private void changUI(int id) {
        switch (id) {
            case R.id.navigation_tab1://首页
                ivMenu3.clearAnimation();
                setImageView(ivMenu1, R.drawable.shouye_icon_selected);
                setImageView(ivMenu2, R.drawable.gongxiangdingdan_icon);
                setImageView(ivMenu4, R.drawable.dindanguanli_icon);
                setImageView(ivMenu5, R.drawable.wode_icon);
                setTextView(tvMenu1, true);
                setTextView(tvMenu2, false);
                setTextView(tvMenu3, false);
                setTextView(tvMenu4, false);
                setTextView(tvMenu5, false);
                break;
            case R.id.navigation_tab2://共享订单
                ivMenu3.clearAnimation();
                setImageView(ivMenu1, R.drawable.shouye_icon);
                setImageView(ivMenu2, R.drawable.gongxiangdingdan_icon_selected);
                setImageView(ivMenu4, R.drawable.dindanguanli_icon);
                setImageView(ivMenu5, R.drawable.wode_icon);
                setTextView(tvMenu1, false);
                setTextView(tvMenu2, true);
                setTextView(tvMenu3, false);
                setTextView(tvMenu4, false);
                setTextView(tvMenu5, false);
                break;
            case R.id.navigation_tab3:
               // ivMenu3.startAnimation(operatingAnim);
                setImageView(ivMenu1, R.drawable.shouye_icon);
                setImageView(ivMenu2, R.drawable.gongxiangdingdan_icon);
                setImageView(ivMenu4, R.drawable.dindanguanli_icon);
                setImageView(ivMenu5, R.drawable.wode_icon);
                setTextView(tvMenu1, false);
                setTextView(tvMenu2, false);
                setTextView(tvMenu3, true);
                setTextView(tvMenu4, false);
                setTextView(tvMenu5, false);
                break;
            case R.id.navigation_tab4://订单管理
               // ivMenu3.clearAnimation();
                setImageView(ivMenu1, R.drawable.shouye_icon);
                setImageView(ivMenu2, R.drawable.gongxiangdingdan_icon);
                setImageView(ivMenu4, R.drawable.dindanguanli_icon_selected);
                setImageView(ivMenu5, R.drawable.wode_icon);
                setTextView(tvMenu1, false);
                setTextView(tvMenu2, false);
                setTextView(tvMenu3, false);
                setTextView(tvMenu4, true);
                setTextView(tvMenu5, false);
                break;
            case R.id.navigation_tab5://我的
                ivMenu3.clearAnimation();
                setImageView(ivMenu1, R.drawable.shouye_icon);
                setImageView(ivMenu2, R.drawable.gongxiangdingdan_icon);
                setImageView(ivMenu4, R.drawable.dindanguanli_icon);
                setImageView(ivMenu5, R.drawable.wode_icon_selected);
                setTextView(tvMenu1, false);
                setTextView(tvMenu2, false);
                setTextView(tvMenu3, false);
                setTextView(tvMenu4, false);
                setTextView(tvMenu5, true);
                break;
            default:
                return;
        }
    }

    private void setImageView(ImageView imageView, int id) {
        imageView.setImageResource(id);
    }

    private void setTextView(TextView textView, boolean icCheck) {
        textView.setTextColor(getResources().getColor(icCheck ? R.color.main_color : R.color.dark_gray));
    }

    @Override
    protected void initListener() {

    }

    private void changeTab(int id) {
        changUI(id);
        if (!fragmentUtil.hasFragment(id)) {
            fragmentUtil.add2Map(newFragment(id), id);
        }
        fragmentUtil.changeTab(id);


    }

    @OnClick({R.id.navigation_tab1, R.id.navigation_tab2, R.id.navigation_tab3, R.id.navigation_tab4, R.id.navigation_tab5})
    void onNavigation(View view) {
        changeTab(view.getId());
    }

    private BaseFragment newFragment(int id) {
        switch (id) {
            case R.id.navigation_tab1://首页
                return new HomeFragment();
            case R.id.navigation_tab2://共享订单
                return new ShareOrderNewFragment();
            case R.id.navigation_tab3:
                return new PublishNewFragment();
            case R.id.navigation_tab4://订单管理
                return new ManagerFragment();
            case R.id.navigation_tab5://我的
                return new MineFragment();
            default:
                return null;
        }

    }

    private final int INTERVAL_TIME = 2000;
    //最近点击返回键时间
    private long lastClickBackTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - lastClickBackTime) > INTERVAL_TIME) {
                ToastUtil.showToast("再按一次退出应用");
                lastClickBackTime = System.currentTimeMillis();
            } else {
//                //退出APP
//                AcCollector.finishAll();
                finish();
            }
        }
        return false;
    }


    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        KLog.e("yangwenwen-onCreate");
//
//    }

    @Subscribe(code = RxBusCode.CHECK_SHARE_ORDER)
    public void chechkShareOrder() {
        changeTab(R.id.navigation_tab4);
    }


    @Subscribe(code = RxBusCode.TO_ShareOrderFragment)
    public void toShareOrderFragment() {
        changeTab(R.id.navigation_tab2);
    }

    @Subscribe(code = RxBusCode.To_PublishFragment)
    public void To_PublishFragment() {
        changeTab(R.id.navigation_tab3);
    }

    @Override
    protected void onDestroy() {

        RxBus.get().unRegister(this);
        super.onDestroy();
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
